//
//  MyStatusPictureView.swift
//  weibo08
//
//  Created by 陈杰明 on 16/3/7.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit
import SDWebImage
//最大列数
private let maxColumn = 3
//间距
private let MyStatusPictureViewCellMargin: CGFloat = 10
//宽高
private let MyStatusPictureViewWH: CGFloat = (UIScreen.width - 2 * MyStatusCellMargin - (CGFloat(maxColumn) - 1) * MyStatusPictureViewCellMargin) / CGFloat(maxColumn)
//CGSize
private let MyStatusPictureViewSize = CGSize(width: MyStatusPictureViewWH, height: MyStatusPictureViewWH)

private let reuseIdentifier = "reuseIdentifier"

class MyStatusPictureView: UICollectionView {
    
    //MARK: - 属性
    var status: MyStatus? {
    
        didSet{
            //计算微博配图大小
        let viewSize = calcviewSize()
            //重新约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(viewSize)
            }
            self.reloadData()
        }
    }
    /**
     计算微博配图大小
     */
    private func calcviewSize() -> CGSize {
    
        let count = status?.storePictureURL?.count ?? 0
        
        if count == 0 {
        
            return CGSizeZero;
            
        }
        if count == 1 {
            var size = CGSize(width: 180, height: 120)
            //获取图片url
            let urlStr = status?.storePictureURL?.first?.absoluteString
            //获取图片
            if let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(urlStr!) {
                //获得图片大小显示
                size = image.size
            }
            //判断图片太大或太小
            if size.width < 40 {
                size.width = 40
            }else if size.width > UIScreen.width * 0.5 {
                size.width = UIScreen.width * 0.5
            }
            
            //cell跟图片一样大
            layout.itemSize = size
            layout.minimumLineSpacing = 0
            layout.minimumInteritemSpacing = 0
        return size
            
        }
        //设置layout
        layout.itemSize = MyStatusPictureViewSize
        layout.minimumLineSpacing = MyStatusPictureViewCellMargin
        layout.minimumInteritemSpacing = MyStatusPictureViewCellMargin
        //其他情况
        //计算列数
        let column = count == 4 ? 2 : count < maxColumn ? count : maxColumn
        //计算行数
        let row = (count + maxColumn - 1) / maxColumn
    
        let width = CGFloat(column) * MyStatusPictureViewWH + (CGFloat(column) - 1) * MyStatusPictureViewCellMargin
        let height = CGFloat(row) * MyStatusPictureViewWH + (CGFloat(row) - 1) * MyStatusPictureViewCellMargin
        return CGSize(width: width, height: height)
    }
    
    
    //布局
    let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    init() {
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        //背景透明
        self.backgroundColor = UIColor.clearColor()
        //设置代理
        self.delegate = self
        self.dataSource = self
        //注册cell
        self.registerClass(MyStatusPictureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
//扩张实现数据源
extension MyStatusPictureView: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.storePictureURL?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyStatusPictureViewCell
        
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.brownColor()
        //如果失败服务器返回的数据有问题
       
        
        cell.imageURL = status?.storePictureURL?[indexPath.item]
        
        return cell
    }
}
//创建cell的class
class MyStatusPictureViewCell: UICollectionViewCell {

    //MARK: - 属性
    var imageURL: NSURL? {
        didSet{
            
            bkgImageView.sd_setImageWithURL(imageURL)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI() {
        //添加子控件
        contentView.addSubview(bkgImageView)
        //添加约束
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        
        //设置填充
        bkgImageView.contentMode = UIViewContentMode.ScaleAspectFill
        //裁剪
        bkgImageView.clipsToBounds = true
        
    }
    
    private lazy var bkgImageView: UIImageView = UIImageView(image: nil)

}

